package com.tonels;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * todo 待调整
 */
public class XmlAnalyse {

    static List<String> wzfzList = new ArrayList<>();
    static List<String> wzqzList = new ArrayList<>();
    static List<String> xxbl = new ArrayList<>();
    static List<String> bkqzList = new ArrayList<>();
    static List<String> bzhz = new ArrayList<>();

    public static final String PATH_Target = "\\\\192.168.1.74\\public\\testts\\tmp";
//    public static final String PATH_Target = "E:\\aiPic\\ls\\ssss";

    public static void main(String[] args) throws IOException {

        Path startingDir = Paths.get(PATH_Target);
        List<Path> result = new LinkedList<Path>();
        Files.walkFileTree(startingDir, new XmlAnalyse.FindJavaVisitor(result));

//        System.out.println(xxbl.size());
    }

    private static class FindJavaVisitor extends SimpleFileVisitor<Path> {
        private List<Path> result;

        public FindJavaVisitor(List<Path> result) {
            this.result = result;
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            if (file.toString().endsWith(".xml")) {
                File file1 = file.toFile();
                try {
                    func(file1);
                } catch (JDOMException e) {
                    e.printStackTrace();
                }
            }
            return FileVisitResult.CONTINUE;
        }
    }


    public static void func(File file) throws IOException, JDOMException {
        //1.创建SAXBuilder对象
        SAXBuilder saxBuilder = new SAXBuilder();
        //2.创建输入流
        InputStream is = new FileInputStream(file);
        //3.将输入流加载到build中
        Document document = saxBuilder.build(is);
        //4.获取根节点
        Element rootElement = document.getRootElement();

        //5.获取子节点
        List<Element> children = rootElement.getChildren();
        for (Element child : children) {
            List<Element> collect = children.stream().filter(e -> "object".equals(e.getName())).collect(Collectors.toList());
            collect.forEach(e -> {
                if (e.getAttribute("name").getValue().equals("血腥画面")) {
                    xxbl.add("血腥画面");
                }
                if (e.getAttribute("name").getValue().equals("武装分子")) {

                }
                if (e.getAttribute("name").getValue().equals("武装枪支")) {

                }
                if (e.getAttribute("name").getValue().equals("爆炸火灾")) {

                }
                if (e.getAttribute("name").getValue().equals("暴恐旗帜")) {

                }
            });
        }
    }

}
